package com.atguigu.tingshu.user.client;

import com.atguigu.tingshu.common.annotation.cache.Cache;
import com.atguigu.tingshu.common.annotation.login.LoginRequired;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Haoran Sun
 * @since 2024/08/21
 */

@RestController
@RequestMapping("client/user/userInfo")
public class UserInfoClientController {

    @Resource
    private UserInfoService userInfoService;

    /**
     * 根据用户id获取用户名称
     *
     * @param id 用户id
     * @return 用户名称
     */
    @GetMapping("/getUserName/{id}")
    @Cache("cache:user:getUserName")
    public String getUserNameById(
            @PathVariable("id") Long id
    ) {
        return userInfoService.getOne(new LambdaQueryWrapper<UserInfo>()
                        .eq(UserInfo::getId, id)
                        .select(UserInfo::getNickname))
                .getNickname();
    }


    /**
     * 根据用户id获取用户信息
     * @param id 用户id
     * @return 用户信息
     */
    @GetMapping("/getUserInfoById/{id}")
    @Cache("cache:user:getUserInfoById")
    UserInfo getUserInfoById(@PathVariable("id")Long id){
        return userInfoService.getById(id);
    }


    /**
     * 获取用户信息
     * @return userInfoVo用户信息
     */
    @GetMapping("/getUserInfoVo")
    @LoginRequired
    Result<UserInfoVo> getUserInfoVo(){
        return Result.ok(userInfoService.getUserInfoVo());
    }

}
